import React from "react";
import type { FormProps } from "antd";
import { Button, Checkbox, Form, Input } from "antd";
import { LockOutlined, UserOutlined } from "@ant-design/icons";
import { useNavigate } from "react-router-dom";
import "./login.scss";
import axios from "../../api/instance";

type FieldType = {
  account?: string;
  password?: string;
  remember?: string;
};

const Login: React.FC = () => {
  const navigate  = useNavigate()
  const onFinish: FormProps<FieldType>["onFinish"] = async (values) => {
    console.log("Success:", values);
    //   axios.post('/login', values).then(res => {
    //     if(res.data.token) {
    //         localStorage.setItem('token', res.data.token)
    //     }
    //   })
    const res = await axios.post("/login", values);
    if (res.data.token) {
      localStorage.setItem("token", res.data.token);
      navigate('/data', {replace: true})
    }
  };
  return (
    <div className="login-box">
      <Form
        className="login-form"
        name="basic"
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 16 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        autoComplete="off"
      >
        <Form.Item<FieldType>
          label="账户名"
          name="account"
          rules={[{ required: true, message: "请输入你的用户名!" }]}
        >
          <Input prefix={<UserOutlined />} />
        </Form.Item>

        <Form.Item<FieldType>
          label="密码"
          name="password"
          rules={[{ required: true, message: "请输入你的密码!" }]}
        >
          <Input.Password prefix={<LockOutlined />} />
        </Form.Item>

        <Form.Item<FieldType>
          name="remember"
          valuePropName="checked"
          label={null}
        >
          <Checkbox>记住密码</Checkbox>
        </Form.Item>

        <Form.Item label={null}>
          <Button type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
};

export default Login;
